<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="姓名（模糊查询）"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="见习组别" prop="groupId">
        <el-select v-model="queryParams.groupId" placeholder="请选择见习组别" clearable size="small">
          <el-option
            v-for="dict in groupIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否合格" prop="isOk">
        <el-select v-model="queryParams.isOk" placeholder="请选择是否合格" clearable size="small">
          <el-option
            v-for="dict in isOkOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="见习时间" prop="examineDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.examineDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择见习时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['person:examine:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['person:examine:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['person:examine:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['person:examine:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="手机号码" align="center" prop="phone"/>
      <el-table-column label="见习组别" align="center" prop="groupId" :formatter="groupIdFormat"/>
      <el-table-column label="见习时间" align="center" prop="examineDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examineDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="isOk" :formatter="isOkFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>

      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <!--      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['person:examine:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['person:examine:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改见习记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员" prop="personId">
          <el-input v-model="tempText" @click.native="showSelect()" placeholder="点击选择"/>
        </el-form-item>
        <el-form-item label="见习组别" prop="groupId">
          <el-select v-model="form.groupId" placeholder="请选择见习组别">
            <el-option
              v-for="dict in groupIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否合格" prop="isOk">
          <el-select v-model="form.isOk" placeholder="请选择是否合格">
            <el-option
              v-for="dict in isOkOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="见习时间" prop="examineDate">
          <el-date-picker clearable size="small"
                          v-model="form.examineDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择见习时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="isShowTransfe" width="40%" append-to-body>
      <el-transfer filterable :titles="['人员列表','已选择']" v-model="peopleSelectList" :data="peopleList"></el-transfer>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okSelect">确 定</el-button>
        <el-button @click="cancelSelect">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listExamine,
  getExamine,
  delExamine,
  addExamine,
  updateExamine,
  exportExamine,
  listForSelect
} from '@/api/person/examine'
import { parseTime } from '@/utils/ruoyi'

export default {
  name: 'Examine',
  data() {
    return {
      tempText: '',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 见习记录表格数据
      examineList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 见习组别字典
      groupIdOptions: [],
      // 是否合格字典
      isOkOptions: [],
      // 查询参数
      queryParams: {
        name: null,
        pageNum: 1,
        pageSize: 10,
        personId: null,
        groupId: null,
        isOk: null,
        examineDate: null
      },
      // 表单参数
      form: {
        examineDate: parseTime(new Date(), '{y}-{m}-{d}')

      },
      // 表单校验
      rules: {},
      isShowTransfe: false,
      peopleList: [],
      peopleSelectList: []
    }
  },
  created() {
    this.getList()
    this.getListForSelect()
    this.getDicts('group').then(response => {
      this.groupIdOptions = response.data
    })
    this.getDicts('is_ok').then(response => {
      this.isOkOptions = response.data
    })
  },
  methods: {
    /** 查询见习记录列表 */
    getList() {
      this.loading = true
      listExamine(this.queryParams).then(response => {
        this.examineList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    getListForSelect() {
      listForSelect(this.queryParams).then(response => {
        response.data.forEach(i => {
          this.peopleList.push({
            key: i.id,
            label: i.name + '(' + i.phone + ')'
          })
        })

      })
    },

    showSelect() {
      this.isShowTransfe = true
      this.peopleSelectList = []
    },

    // 见习组别字典翻译
    groupIdFormat(row, column) {
      return this.selectDictLabel(this.groupIdOptions, row.groupId)
    },
    // 是否合格字典翻译
    isOkFormat(row, column) {
      return this.selectDictLabel(this.isOkOptions, row.isOk)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    cancelSelect() {
      this.isShowTransfe = false
      this.peopleSelectList = []
    },
    okSelect() {
      //确认选择
      this.isShowTransfe = false
      console.log(this.peopleSelectList)

      this.tempText = `已选择【` + this.peopleSelectList.length + `】人`
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        personId: null,
        groupId: null,
        isOk: null,
        remark: null,
        examineDate: parseTime(new Date(), '{y}-{m}-{d}'),
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      }
      this.tempText = ''
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加见习记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExamine(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改见习记录'
        this.tempText = response.data.name
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExamine(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            //personIdList
            if (this.peopleSelectList == 0) {
              this.$message.error('还未选择见习人员')
              return
            }
            this.form.personIdList = this.peopleSelectList
            addExamine(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除见习记录编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delExamine(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有见习记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true
        return exportExamine(queryParams)
      }).then(response => {
        this.download(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    }
  }
}
</script>
